/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-07 16:24:55
 * describe:打开新浏览器
 */
APP.controller('OpenBrowserController', ['$scope', 'OpenBrowserService',
  function ($scope, OpenBrowserService) {

    /***变量声明***/
    $scope.data = undefined;

    /**
     * 初始化函数
     */
    $scope.init = function () {
      $scope.data = {
        url: ''
      };
    };

    /**
     * 打开新页面
     */
    $scope.open = function () {
      OpenBrowserService.openNewPage($scope.data.url, function () {
        console.log('关闭');
      });
    };


    /*
     $scope.init();//如果页面缓存则用此方法初始化
     */
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.init();
    });
  }
]);

/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-07 16:24:55
 * describe:打开新浏览器
 */
APP.service('OpenBrowserService', ['$http', 'UrlService', function ($http, UrlService) {
  /**
   * 打开新页面
   * @param url  新地址url
   * @param exit 退出方法
   */
  this.openNewPage = function (url, exit) {
    var w = window.open(url, '_blank', 'location=yes');
    if (w) {
      if (CONFIG.platform != 'wap') {
        w.addEventListener('exit', function (event) {
          w = undefined;
          exit();
        });
      } else {
        var loop = setInterval(function () {
          if (w.closed) {
            clearInterval(loop);
            exit();
          }
        }, 1000);
      }
    }
  }

}]);
